//
//  ViewController.m
//  WKWebViewTest
//
//  Created by jing ling on 2018/1/25.
//  Copyright © 2018年 jing ling. All rights reserved.
//

#import "ViewController.h"
#import "WKViewController.h"
@import WebKit;
@interface ViewController ()<WKNavigationDelegate>
@property (nonatomic, strong) WKWebView *wkWebView;

@property (nonatomic, strong) UIButton *backBtn;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configWKWebView];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSString *urlStr = @"https://wawa-api.vchangyi.com/api/duiba/login?ts=1516869170065&_version=2.2.0.0&_os=ios";
    NSString *cookies = @"Qk8gcQdbt4DoQ5FktnpNqw%2Fo6Ys3Pvpw6Avgl8sZtkdlRs6QcVxjrTkhg8JjMhqntlszqrGJTITV2WpmkjDHoY4%2BGzenpwMoNHylc2ZsC6JzvEYeb5hBA9XqX%2B6W7ySiPCEP%2FekmraeWX6tH%2BUwV1Q%3D%3D";
    
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    NSString *auth_data = [NSString stringWithFormat:@"auth_data=%@", cookies];
    [request addValue:auth_data forHTTPHeaderField:@"Cookie"];
    [self.wkWebView loadRequest:request];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)configWKWebView {
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    WKUserContentController *userContentCtr = [[WKUserContentController alloc] init];
    config.userContentController = userContentCtr;
    self.wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
    [self.view addSubview:self.wkWebView];
    self.wkWebView.navigationDelegate = self;
    
    
    
}

- (void)backClick:(UIButton *)btn {
    if (self.wkWebView.canGoForward) {
        [self.wkWebView goForward];
    }
}

#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation {
//    if (webView.canGoForward) {
//        if (self.backBtn == nil) {
//            self.backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//            self.backBtn.frame = CGRectMake(10, 30, 50, 50);
//            self.backBtn.backgroundColor = [UIColor orangeColor];
//            [self.backBtn addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
//            [self.view addSubview:self.backBtn];
//        }
//    } else {
//        if (self.backBtn) {
//            self.backBtn = nil;
//        }
//    }
    NSLog(@"start");
}

- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation {
    NSLog(@"commit");
    NSLog(@"%@-%@", webView, navigation);
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
    
    
}

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
    
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    NSLog(@"%@", navigationAction);
    if ([navigationAction.request.URL.host isEqualToString:@"www.duiba.com.cn"]) {
        
        WKViewController *vc = [[WKViewController alloc] init];
        vc.url = navigationAction.request.URL;
        [self.navigationController pushViewController:vc animated:YES];
        decisionHandler(WKNavigationActionPolicyCancel);
    } else {
        decisionHandler(WKNavigationActionPolicyAllow);
    }
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
    NSLog(@"%@", navigationResponse);
    decisionHandler(WKNavigationResponsePolicyAllow);
}

- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation {
    NSLog(@"%@-%@", webView, navigation);
}

@end
